package org.ucas.faker.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.ucas.faker.web.entity.Depot;
import org.ucas.faker.web.entity.TaskLog;
import org.ucas.faker.web.fetcher.FetchExecutor;
import org.ucas.faker.web.service.TaskService;
import org.ucas.faker.web.utils.model.Context;
import org.ucas.faker.web.entity.User;
import org.ucas.faker.web.utils.wx.WxClient;
import org.ucas.faker.web.service.DepotService;
import org.ucas.faker.web.service.PatentService;
import org.ucas.faker.web.service.UserService;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

/**
 * Created by suemi on 17-7-9.
 */
public abstract class BaseController {

  @Autowired
  protected HttpSession session;

  @Autowired
  protected DepotService depotService;

  @Autowired
  protected UserService userService;

  @Autowired
  protected PatentService patentService;

  @Autowired
  protected TaskService taskService;

  @Autowired
  protected WxClient wxClient;

  @Autowired
  protected FetchExecutor fetcher;


  protected Context context() {
    Context ctx =  (Context) session.getAttribute("context");
    if(ctx == null) {
      ctx = new Context();
      session.setAttribute("context", ctx);
    }
    return ctx;
  }

  protected Depot currentDepot() {
    Context ctx = context();
    return ctx == null? null : ctx.depotId == null? null : depotService.load(ctx.depotId);
  }

  protected User currentUser() {
    Context ctx = context();
    return ctx == null? null : ctx.userId == null? null : userService.load(ctx.userId);
  }

  protected Long currentUserId() {
    Context ctx = context();
    return ctx == null? null : ctx.userId;
  }

  protected Long currentDepotId() {
    Context ctx = context();
    return ctx == null? null : ctx.depotId;
//    return 29L;
  }

  protected TaskLog currentTask(TaskLog.TaskCategory category) {
    Depot depot = currentDepot();
    if(depot == null) return null;
    return taskService.load(depot.getId(),depot.getVersion(),category);
  }

  protected Long currentTaskId(TaskLog.TaskCategory category) {
    TaskLog task = currentTask(category);
    return task == null? null : task.getId();
  }

  protected String currentQuery() {
    Context ctx = context();
    return ctx == null? null:ctx.query;
  }
  
  protected Long currentTreeId() {
	if((Long) session.getAttribute("treeId")!=null) {
		return (Long) session.getAttribute("treeId");
	}
    return 3L;
  }
  protected void currentTreeId(Long treeId) {
    session.setAttribute("treeId", treeId);
  }
  
  protected TaskLog buildTask(TaskLog.TaskCategory category) {
    Depot depot = currentDepot();
    TaskLog task = new TaskLog();
    task.setCategory(category);
    if(depot != null) task.setDepotId(depot.getId());
    task.setStatus(TaskLog.TaskStatus.CREATED);
    task.setOperator(currentUserId());
    if(depot != null) task.setVersion(depot.getVersion());

    return task;
  }
  
  protected String searchQuery() {
    return (String)session.getAttribute("searchQuery");
  }

  protected void searchQuery(String searchQuery) {
    session.setAttribute("searchQuery", searchQuery);
  }


  protected List<String> keywords() {
	return (List<String>) session.getAttribute("keywords");
  }
  protected void keywords(List<String> keywords) {
    session.setAttribute("keywords", keywords);
  }
  
  protected List<String> columns() {
	return (List<String>) session.getAttribute("columns");
  }
  protected void columns(List<String> columns) {
    session.setAttribute("columns", columns);
  }
  protected String graph_json() {
	return (String)session.getAttribute("graph_json");
  }
  protected void graph_json(String graph_json) {
    session.setAttribute("graph_json", graph_json);
  }
  protected String callback_json() {
	return (String)session.getAttribute("callback_json");
  }
  protected void callback_json(String callback_json) {
    session.setAttribute("callback_json", callback_json);
  }
  protected String graph_type() {
	return (String)session.getAttribute("graph_type");
  }
  protected void graph_type(String graph_type) {
    session.setAttribute("graph_type", graph_type);
  }
  protected void graph_ids_map(HashMap<String,List<Long>> graph_ids_map) {
    session.setAttribute("graph_ids_map", graph_ids_map);
  }
  protected HashMap<String,List<Long>> graph_ids_map() {
    return (HashMap<String,List<Long>>)session.getAttribute("graph_ids_map");
  }
  protected void similar_query(String similar_query) {
    session.setAttribute("similar_query", similar_query);
  }
  protected String similar_query() {
    return (String)session.getAttribute("similar_query");
  }
}
